package com.puzzlebazar.client.main.control;

import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.ClosingHandler;


public abstract class RootControllerImpl extends GenericControllerImpl implements ClosingHandler {

	private String confirmLeaveState = null;
	private HandlerRegistration windowClosingHandlerRegistration = null;

	@Override
	public boolean confirmLeaveState() {
		if( confirmLeaveState == null ) return true;		
		return Window.confirm( confirmLeaveState );		
	}
	
	@Override
	public void setConfirmStateChange(String confirmLeaveState) {
		if( confirmLeaveState == null && this.confirmLeaveState == null ) return;
		if( confirmLeaveState != null && this.confirmLeaveState == null )
			windowClosingHandlerRegistration = Window.addWindowClosingHandler(this);	
		if( confirmLeaveState == null && this.confirmLeaveState != null 
				&& windowClosingHandlerRegistration != null )
			windowClosingHandlerRegistration.removeHandler();	
		this.confirmLeaveState = confirmLeaveState;
	}

	@Override
	public void onWindowClosing(Window.ClosingEvent event) {
		event.setMessage(confirmLeaveState);
	}
	
}